package com.jinrustar.weimaqi.util

import java.util.HashMap

/**
 * Created by liduanwei on 2017/10/26 0026. liduanwei1988@gmail.com
 */
class ReflectUtil {
    private val testName = "xxx"

    companion object {

        fun getAllFieldsAndValue(clazz: Class<*>, instance: Any, map: MutableMap<String, Any>) {
            if (clazz.superclass == null) {
                return
            }
            getAllFieldsAndValue(clazz.superclass, instance, map)

            val fields = clazz.declaredFields
            for (f in fields) {
                f.isAccessible = true
                try {
                    map.put(f.name, f.get(instance))
                } catch (e: IllegalAccessException) {
                    e.printStackTrace()
                }

            }
        }


        @JvmStatic
        fun main(args: Array<String>) {
            val reflectUtil = ReflectUtil()
            val map = HashMap<String, Any>()
            getAllFieldsAndValue(reflectUtil.javaClass, reflectUtil, map)
            println(map)
        }
    }

}
